package com.h3c.nem.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eos.common.util.Result;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.h3c.nem.dto.TbTeacherCertificationInfoDTO;
import com.h3c.nem.entity.SdDept;
import com.h3c.nem.service.CommService;
import com.h3c.nem.service.ISdDeptService;
import com.h3c.nem.service.ITbTeacherService;
import com.h3c.nem.service.TbTeacherCertificationInfoService;
import com.h3c.nem.utils.BusiEnumDefine;
import com.h3c.nem.utils.ReturnCodeData;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: yys44482
 * @Date: 2022/7/26
 */
@Api(tags = " TbTeacherCertificationInfo 接口")
@Slf4j
@CrossOrigin
@RestController
@RequestMapping(value = "/tbTeacherCertificationInfo",produces = "application/json;charset=utf-8")
public class TbTeacherCertificationInfoController {

    @Autowired
    TbTeacherCertificationInfoService tbTeacherCertificationInfoService;

    @Autowired
    CommService commService;

    @Autowired
    ISdDeptService sdDeptService;

    @Autowired
    ITbTeacherService tbTeacherService;


    /**
     * 模糊列表查询
     * @param paramsDTO
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping(value = "/queryFuzzyListForPage")
    public ResponseEntity<JSONObject> queryFuzzyListForPage(@ModelAttribute TbTeacherCertificationInfoDTO paramsDTO,
                                                            @RequestParam(name = "pageNum", required = true) Integer pageNum,
                                                            @RequestParam(name = "pageSize", required = true) Integer pageSize) {
        log.info("准备执行  列表分页模糊查询，传入的参数为：" + JSON.toJSONString(paramsDTO));
        JSONObject data = new JSONObject();
        List<String> deptIdList = new ArrayList<>();
        if (StringUtils.isEmpty(paramsDTO.getDeptId())){
            List<SdDept> sdDepts = sdDeptService.list();
            List<Integer> allDeptIds = sdDepts.stream().map(SdDept::getDeptId).collect(Collectors.toList());
            for (Integer allDeptId : allDeptIds) {
                deptIdList.add(String.valueOf(allDeptId));
            }
            String deptIds = String.join(",", deptIdList);
            paramsDTO.setDeptId(deptIds);
        }
        String loginUserId = commService.getLoginUserId();
        List<SdDept> deptList = sdDeptService.list(new QueryWrapper<SdDept>().like("dept_manager_id", loginUserId));
//        paramsDTO.setDept(dept);
        paramsDTO.setDeptList(deptList);
        try {
            //分页
            Page page = PageHelper.startPage(pageNum, pageSize);
            //执行
            List<TbTeacherCertificationInfoDTO> resultDTOList = new ArrayList<>();
            if (commService.checkLoginUserRole(BusiEnumDefine.USER_ROLE_DEPT_MANAGER)){
                resultDTOList = tbTeacherCertificationInfoService.queryTbTeacherCertificationInfo(paramsDTO);
            }
            PageInfo<TbTeacherCertificationInfoDTO> pageInfo = page.toPageInfo();
            pageInfo.setList(resultDTOList);
            data.put("data",pageInfo);
            data.put("msg", "列表分页模糊查询成功");
            data.put("code", ReturnCodeData.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("执行列表分页模糊查询异常，异常信息为：" + JSON.toJSONString(e));
            data.put("msg", e.toString());
            data.put("code", ReturnCodeData.FAIL);
        }
        log.info("执行列表分页模糊查询完成，准备返回的结果为：" + JSON.toJSONString(data));
        return new ResponseEntity<>(data, HttpStatus.ACCEPTED);
    }

    /**
     * 发起导师认证
     * @param paramsDTO
     * @return
     */
    @PostMapping("/sendCert")
    public Result sendCert(@RequestBody TbTeacherCertificationInfoDTO paramsDTO) {
        tbTeacherCertificationInfoService.sendCert(paramsDTO);
        return new Result(true, 20000, "发起认证成功，可查看[导师认证与资质管理].");
    }
}
